package jp.co.nikkeibp.android.BombGame;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class SettingsActivity extends Activity {
	private int H;
	private int W;
	private int bombNum;
	private LinearLayout linearLayout;
	private TextView textViewH;
	private TextView textViewW;
	private TextView textViewBombNum;
	private EditText editTextH;
	private EditText editTextW;
	private EditText editTextBombNum;
	private Button buttonOk;
	private Button buttonCancel;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//現在の設定値を取得
		SharedPreferences pref = getSharedPreferences("pref", MODE_PRIVATE);
		H  = pref.getInt("H", 9);
		W = pref.getInt("W", 9);
		bombNum = pref.getInt("bombNum", 5);
		//部品の取得
		linearLayout = new LinearLayout(this);//縦か横に並べるレイアウト
		linearLayout.setOrientation(LinearLayout.VERTICAL);//縦に並べる
		textViewH = new TextView(this);
		textViewW = new TextView(this);
		textViewBombNum = new TextView(this);
		editTextH = new EditText(this);
		editTextW = new EditText(this);
		editTextBombNum = new EditText(this);
		buttonOk = new Button(this);
		buttonCancel = new Button(this);
		//ラベルの設定
		textViewH.setText("高さ：");
		textViewW.setText("幅：");
		textViewBombNum.setText("爆弾の数：");
		buttonOk.setText("OK");
		buttonCancel.setText("キャンセル");
		//初期値の設定
		editTextH.setText(String.valueOf(H));
		editTextW.setText(String.valueOf(W));
		editTextBombNum.setText(String.valueOf(bombNum));
		//部品の貼り付け
		linearLayout.addView(textViewH);
		linearLayout.addView(editTextH);
		linearLayout.addView(textViewW);
		linearLayout.addView(editTextW);
		linearLayout.addView(textViewBombNum);
		linearLayout.addView(editTextBombNum);
		linearLayout.addView(buttonOk);
		linearLayout.addView(buttonCancel);
		setContentView(linearLayout);
		//OKボタンのイベント処理
		buttonOk.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				try {
					H = Integer.parseInt(editTextH.getText().toString());
					W = Integer.parseInt(editTextW.getText().toString());
					bombNum = Integer.parseInt(editTextBombNum.getText().toString());
					SharedPreferences pref = getSharedPreferences("pref", MODE_PRIVATE);
					Editor editor = pref.edit();
					editor.putInt("H", H);
					editor.putInt("W", W);
					editor.putInt("bombNum", bombNum);
					editor.commit();
					Intent intent = new Intent(SettingsActivity.this, BombGameActivity.class);
					SettingsActivity.this.startActivity(intent);
				} catch (NumberFormatException e) {
					 Toast.makeText(SettingsActivity.this, "半角数字を入力してください", Toast.LENGTH_SHORT).show();
				}
			}
		});
		//キャンセルボタンの場合は
		buttonCancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				finish();//Activityを終了させる
			}
		});
	}
}
